--[[
Author: your name
Date: 2020-12-01 11:04:21
LastEditTime: 2020-12-01 20:51:43
LastEditors: Please set LastEditors
Description: In User Settings Edit
FilePath: \Lua\View\CharactorUI\UICharactorPreSkillview.lua
--]]
require 'Common/define'
require 'Logic/Config'
require 'Common/functions'

UICharactorPreSkillview = {};
HelperFunc.SetUIEnv(UICharactorPreSkillview)
local this = UICharactorPreSkillview;

this.currentHeroData = nil;
--this.isMyself;

this.gameObject = nil;
this.transform = nil;
this.LuaBehaviour = nil;

this.hasInit = false;

this.trickGroup = {};
this.specialGroup={};
this.specialTwoGroup ={};
this.passiveGroup = {};
this.skill5Group = {}
this.tf_btn = nil;

function UICharactorPreSkillview.Init(heroData)--todo 应该小写 暂时先大写
    currentHeroData = heroData
    --isMyself = logicMgr.HeroManager.isMyself(currentHeroData.partnerModelId);
    local name,package = getUIName(PanelResNames.UICharactorPreSkillview);
    panelMgr:ShowUI(name,package, this.onCreate, Config.useLocalUI);
end

function UICharactorPreSkillview.onCreate(obj)
    --gameObject = obj;
    --transform = obj.transform;
    --LuaBehaviour = transform:GetComponent('LuaBehaviour');

    if not hasInit then
        hasInit = true;
        local temp = {"TrickGroup","SpecailGroup","SpecailTwoGroup","PassiveGroup","skill5"}
        for k,v in pairs(temp) do
            local tb = nil
            if k == 1 then
                tb = trickGroup
            elseif k == 2 then
                tb = specialGroup
            elseif k == 3 then
                tb = specialTwoGroup
            elseif k == 4 then
                tb = passiveGroup
            else
                tb = skill5Group
            end
            tb.btn = obj:GetChild(v)
            tb.icon = tb.btn:GetChild("Mask"):GetChild("Icon")
            tb.objLock = tb.btn:GetController("CtrlLock")
            tb.txtLock = tb.btn:GetChild("LockText")
            tb.objLv = tb.btn:GetController("CtrlLv")
            tb.txtLv = tb.btn:GetChild("SkillLevelText")
            tb.ctrlRace = tb.btn:GetController("race")
            tb.ctrlFifthSkill = tb.btn:GetController("fifthSkill")
            if k == 3 or k == 4 then
                tb.btnSwitch = tb.btn:GetChild("SwitchBtn")
            end
        end
        this.ctrlSkillRace = obj:GetController("race")
        this.ctrlSkillFifth = obj:GetController("fifthSkill")
        this.addEvent();
        tf_btn = obj:GetChild("tf_Btn");
        if tf_btn then
            tf_btn.visible = false;
        end
    end

    this.refresh();
end

function UICharactorPreSkillview.addEvent()
    trickGroup.btn.onClick:Add(this.onClickShowTrickSkill)
    specialGroup.btn.onClick:Add(this.onClickShowSpecialSkill)
    specialTwoGroup.btn.onClick:Add(this.onClickShowSpecialTwoSkill)
    specialTwoGroup.btnSwitch.onClick:Add(this.onClickSwitchSpecialTwo)
    passiveGroup.btn.onClick:Add(this.onClickShowPassiveSkill)
    passiveGroup.btnSwitch.onClick:Add(this.onClickShowPassiveSkill)
    skill5Group.btn.onClick:Add(this.OnClickSkill5)
end

function UICharactorPreSkillview.removeEvent()
    trickGroup.btn.onClick:Clear()
    specialGroup.btn.onClick:Clear()
    specialTwoGroup.btn.onClick:Clear()
    specialTwoGroup.btnSwitch.onClick:Clear()
    passiveGroup.btn.onClick:Clear()
    passiveGroup.btnSwitch.onClick:Clear()
    skill5Group.btn.onClick:Clear()
end

function UICharactorPreSkillview.LocalListenEvent()
    ListenEvent(Config.EventType.Fresh_Skill,this.reGetData)
    ListenEvent(Config.EventType.Fresh_Star,this.reGetData)
end

function UICharactorPreSkillview.LocalCloseEvent()
    CloseEvent(Config.EventType.Fresh_Skill,this.reGetData)
    CloseEvent(Config.EventType.Fresh_Star,this.reGetData)
end

function UICharactorPreSkillview.OnDestroy()
    this.removeEvent();
end

function UICharactorPreSkillview.OnEnable()
    this.LocalListenEvent();
end

function UICharactorPreSkillview.OnDisable()
    this.LocalCloseEvent();
end

function UICharactorPreSkillview.reGetData()
    if currentHeroData~=nil then
        currentHeroData = logicMgr.HeroManager.getHeroDatabyPartnerId(tostring(currentHeroData["partnerModelId"]),true);
        this.refresh();
    end
end

function UICharactorPreSkillview.refresh()
    local skillInfos= logicMgr.SkillManager.getSkillInfosByModelId(currentHeroData.partnerModelId);
    this.refreshSkillIcon(skillInfos[EnumConst.SkillFieldType.Trick],trickGroup);
    this.refreshSkillIcon(skillInfos[EnumConst.SkillFieldType.Speical],specialGroup);
    this.refreshSkillIcon(skillInfos[EnumConst.SkillFieldType.SpecialTwo],specialTwoGroup);
    this.refreshSkillIcon(skillInfos[EnumConst.SkillFieldType.Passive],passiveGroup);
    this.refreshSkillIcon(skillInfos[EnumConst.SkillFieldType.skill5],skill5Group,EnumConst.SkillFieldType.skill5);
    this.ctrlSkillRace.selectedIndex = currentHeroData.faction
    if skillInfos[EnumConst.SkillFieldType.skill5] then
        this.ctrlSkillFifth.selectedIndex = 1
    else
        this.ctrlSkillFifth.selectedIndex = 0
    end
end

function UICharactorPreSkillview.refreshSkillIcon(skillInfoItem,curGroup,index)

    if skillInfoItem == nil then
        curGroup["skill"] = nil;
        curGroup["level"] = 0;
        curGroup["btn"].visible = false;
        return;
    end

    curGroup["btn"].visible = true;

    --local curLv = logicMgr.SkillManager.getSkillLv(skillInfoItem,currentHeroData.star); --当前等级
    local curLv = 4--#decodeJsonStrToTabel(skillInfoItem.f_LevelRequireRank);
    curGroup["skill"] = skillInfoItem;
    curGroup["level"] = curLv;
    local unlock = curLv>0;
    curGroup.objLock.selectedIndex = not unlock and 1 or 0
    curGroup.objLv.selectedIndex = unlock and 1 or 0
    --if curLv>0 then
        curGroup.txtLv.text = tostring(curLv);
    --else
    --    local lvStars = decodeJsonStrToTabel(skillInfoItem["f_LevelRequireRank"]);
    --    curGroup.txtLock.text =string.format(getLanguage("Sys_UnlockHeroRank"),tostring(lvStars[1]/10000));

    --end
    if curGroup["btnSwitch"]~=nil then
        --curGroup.btnSwitch.gameObject:SetActive(unlock and isMyself);
        curGroup.btnSwitch.visible = false
    end
    local iconName = tostring(skillInfoItem["f_SkillIcon"]);
    curGroup.icon.url = EnumConst.AtlasPath.SkillAtlas..iconName
    --resMgr:LoadIconFromPrefab("skillAtlas",iconName,function (sp)
    --    curGroup.icon.sprite = sp;
    -- end)
    if index == EnumConst.SkillFieldType.skill5 then
        local faction = currentHeroData.faction
        if faction == 1 or faction == 2 or faction == 3 then
            curGroup.ctrlRace.selectedIndex = faction
            curGroup.ctrlFifthSkill.selectedIndex = 1
        else
            curGroup.ctrlRace.selectedIndex = 0
        end
    end
end

function UICharactorPreSkillview.onClickShowTrickSkill()
    if trickGroup["skill"]~=nil then
        showUI(PanelNames.UICharactorInfoHeroSkillDetails,trickGroup["skill"],trickGroup["level"],currentHeroData.partnerModelId,{trickGroup,specialGroup,specialTwoGroup,passiveGroup,skill5Group},true);
    end
end


function UICharactorPreSkillview.onClickShowSpecialSkill()
    if specialGroup["skill"]~=nil then
        showUI(PanelNames.UICharactorInfoHeroSkillDetails,specialGroup["skill"],specialGroup["level"],currentHeroData.partnerModelId,{trickGroup,specialGroup,specialTwoGroup,passiveGroup,skill5Group},true);
    end
end


function UICharactorPreSkillview.onClickShowSpecialTwoSkill()
    if specialTwoGroup["skill"]~=nil then
        showUI(PanelNames.UICharactorInfoHeroSkillDetails,specialTwoGroup["skill"],specialTwoGroup["level"],currentHeroData.partnerModelId,{trickGroup,specialGroup,specialTwoGroup,passiveGroup,skill5Group},true);
    end
end


function UICharactorPreSkillview.onClickSwitchSpecialTwo()
    if #logicMgr.SkillManager.getImitationSkillInfos(EnumConst.SkillFieldType.SpecialTwo)>0 then
        showUI(PanelNames.UICharactorInfoHeroSkillSelect,specialTwoGroup["skill"],EnumConst.SkillFieldType.SpecialTwo);
    else
        UISysTips.AddMsg(getLanguage("Sys_MsgUnableMocking"));
    end
end


function UICharactorPreSkillview.onClickShowPassiveSkill()
    if passiveGroup["skill"] then
        showUI(PanelNames.UICharactorInfoHeroSkillDetails,passiveGroup["skill"],passiveGroup["level"],currentHeroData.partnerModelId,{trickGroup,specialGroup,specialTwoGroup,passiveGroup,skill5Group},true);
    else
        UISysTips.AddMsg(getLanguage("Sys_MsgUnableMocking"));
    end
end
function UICharactorPreSkillview.OnClickSkill5()
    if skill5Group["skill"] then
        showUI(PanelNames.UICharactorInfoHeroSkillDetails, skill5Group["skill"], skill5Group["level"],currentHeroData.partnerModelId,{trickGroup,specialGroup,specialTwoGroup,passiveGroup,skill5Group},true);
    else
        UISysTips.AddMsg(getLanguage("Sys_MsgUnableMocking"));
    end
end

function UICharactorPreSkillview.onClickSwitchPassive()
    if #logicMgr.SkillManager.getImitationSkillInfos(EnumConst.SkillFieldType.Passive)>0 then
        showUI(PanelNames.UICharactorInfoHeroSkillSelect,passiveGroup["skill"],EnumConst.SkillFieldType.Passive);
    else
        UISysTips.AddMsg(getLanguage("Sys_MsgUnableMocking"));
    end
end

return UICharactorPreSkillview;
